var express = require("express");
const query = require("./db");
var router = express.Router();
router.get("/", function (req, res, next) {
  res.send("测试接口");
});

// 接口
router.get("/test", function (req, res, next) {
  let sql = "select * from `tb_admin`";
  // res.send('测试接口1')
  query(sql, function (err, rows) {
    if (err) {
      var data = {
        code: -1,
        data: null,
        isSuccess: false,
        msg: err.sqlMessage,
      };
    } else {
      var data = {
        code: 0,
        data: rows,
        isSuccess: true,
        msg: "请求成功",
      };
    }
    res.json(data);
  });
});
// 测试接口
router.get("/add", function (req, res, next) {
  let sql =
    "insert into `tb_staff` (staff_id,staff_name,sex,password) values('201761311','啊啊','女','a123123','计算机科学与技术','2017','5')";
  // res.send('测试接口1')
  query(sql, function (err, rows) {
    console.log(rows);
    if (err) {
      var data = {
        code: -1,
        data: null,
        isSuccess: false,
        msg: err.sqlMessage,
      };
    } else {
      var data = {
        code: 0,
        data: rows,
        isSuccess: true,
        msg: "请求成功",
      };
    }
    res.json(data);
  });
});
// 管理员登录
router.get("/adminLogin", (req, res, next) => {
  console.log("query", req.query);
  const { admin_id, password } = req.query;
  let sql =
    "select admin_id as '管理员' from `tb_admin` where admin_id='" +
    admin_id +
    "' and password='" +
    password +
    "'";
  console.log(sql);
  query(sql, (err, rows) => {
    let data = {};
    if (err) {
      data = {
        code: -1,
        data: null,
        isSuccess: false,
        msg: err.sqlMessage,
      };
    } else {
      if (rows.length > 0) {
        console.log("rows", rows);
        data = {
          code: 1,
          data: rows,
          isSuccess: true,
          msg: "登录成功",
        };
      } else {
        data = {
          code: 0,
          isSuccess: false,
          msg: "用户名或密码错误！",
        };
      }
    }
    res.json(data);
  });
});

// 增加员工
router.get("/addStaff", (req, res, next) => {
  console.log("query", req.query);
  const {
    staff_id,
    staff_name,
    sex,
    birth,
    department,
    rank,
    nation,
    native_place,
    join_date,
    role,
  } = req.query;
  let sql = `insert into tb_staff (staff_id,password,staff_name,sex,birth,department,rank,nation,native_place,join_date,role) values('${staff_id}','${staff_name}','${staff_name}','${sex}','${birth}','${department}','${
    (rank, nation, native_place, join_date, role)
  }')`;
  console.log(sql);
  query(sql, (err, rows) => {
    let data = {};
    if (err) {
      data = {
        code: -1,
        data: null,
        isSuccess: false,
        msg: err.sqlMessage,
      };
    } else {
      if (rows.length > 0) {
        console.log("rows", rows);
        data = {
          code: 1,
          data: rows,
          isSuccess: true,
          msg: "登录成功",
        };
      } else {
        data = {
          code: 0,
          isSuccess: false,
          msg: "用户名或密码错误！",
        };
      }
    }
    res.json(data);
  });
});

// 员工登录
router.get("/staffLogin", (req, res, next) => {
  console.log("query", req.query);
  const { staff_id, password } = req.query;
  let sql =
    "select * from `tb_staff` where staff_id='" +
    staff_id +
    "' and password='" +
    password +
    "'";
  console.log(sql);
  query(sql, (err, rows) => {
    let data = {};
    if (err) {
      data = {
        code: -1,
        data: null,
        isSuccess: false,
        msg: err.sqlMessage,
      };
    } else {
      if (rows.length > 0) {
        console.log("rows", rows);
        data = {
          code: 1,
          data: rows,
          isSuccess: true,
          msg: "登录成功",
        };
      } else {
        data = {
          code: 0,
          isSuccess: false,
          msg: "用户名或密码错误！",
        };
      }
    }
    res.json(data);
  });
});

module.exports = router;
